<template>
  sdf
    <div class="user-list">
      <el-card class="box-card">
        <template #header>
          <div class="card-header">
            <span>用户列表</span>
            <el-button type="primary" @click="showAddDialog">添加用户</el-button>
          </div>
        </template>
        
        <el-table :data="users" style="width: 100%">
          <el-table-column prop="id" label="ID" width="80" />
          <el-table-column prop="username" label="用户名" />
          <el-table-column prop="email" label="邮箱" />
          <el-table-column label="操作" width="180">
            <template #default="scope">
              <el-button size="small" @click="viewUser(scope.row)">查看</el-button>
              <el-button size="small" type="danger" @click="deleteUser(scope.row)">删除</el-button>
            </template>
          </el-table-column>
        </el-table>
      </el-card>
  
      <!-- 添加用户对话框 -->
      <el-dialog v-model="dialogVisible" title="添加用户">
        <el-form :model="form" label-width="120px">
          <el-form-item label="用户名">
            <el-input v-model="form.username" />
          </el-form-item>
          <el-form-item label="密码">
            <el-input v-model="form.password" type="password" />
          </el-form-item>
          <el-form-item label="邮箱">
            <el-input v-model="form.email" />
          </el-form-item>
        </el-form>
        <template #footer>
          <span class="dialog-footer">
            <el-button @click="dialogVisible = false">取消</el-button>
            <el-button type="primary" @click="submitForm">确定</el-button>
          </span>
        </template>
      </el-dialog>
    </div>
  </template>
  
  <script setup lang="ts" name="UserList">
  import { ref, onMounted } from 'vue'
  import { userApi } from '@/api/user/user'
  import { ElMessage, ElMessageBox} from 'element-plus'
  
  const users = ref([])
  const dialogVisible = ref(false)
  const form = ref({
    username: '',
    password: '',
    email: ''
  })
  
  const loadUsers = async () => {
    try {
      const response = await userApi.getUsers()
      console.log(response)
      users.value = response.data
    } catch (error) {
      ElMessage.error('获取用户列表失败')
    }
  }
  
  const showAddDialog = () => {
    form.value = {
      username: '',
      password: '',
      email: ''
    }
    dialogVisible.value = true
  }
  
  const submitForm = async () => {
    try {
      await userApi.createUser(form.value)
      ElMessage.success('添加用户成功')
      dialogVisible.value = false
      loadUsers()
    } catch (error) {
      ElMessage.error('添加用户失败')
    }
  }
  
  const viewUser = async (user: { id: any; }) => {
    try {
      const response = await userApi.getUserById(user.id)
      ElMessage.info(`用户信息：${JSON.stringify(response.data.data)}`)
    } catch (error) {
      ElMessage.error('获取用户详情失败')
    }
  }
  
  const deleteUser = async (user: { id: any; }) => {
      try {
          await ElMessageBox.confirm('确认删除该用户吗？', '提示', {
              confirmButtonText: '确定',
              cancelButtonText: '取消',
              type: 'warning'
          })
          await userApi.deleteUser(user.id)
          ElMessage.success('删除成功')
          loadUsers()
      } catch (error) {
          if (error !== 'cancel') {
              ElMessage.error('删除失败')
          }
      }
  }
  
  onMounted(() => {
    loadUsers()
  })
  </script>
  
  <style scoped>
  .user-list {
    padding: 20px;
  }
  .card-header {
    display: flex;
    justify-content: space-between;
    align-items: center;
  }
  </style>